package com.wu.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wu.bean.Result;
import com.wu.bean.UserBean;
import com.wu.bean.exception.UserNotFoundException;
import com.wu.service.UserService;
import com.wu.system_service.EmailService;
import com.wu.system_service.TokenCache;
import com.wu.util.BaseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName UserController
 * @Author wuxuan
 * @Date 2019/3/2 17:12
 * @Version 1.0
 * @Description
 **/
@RestController
@Scope("prototype")
@Slf4j
public class UserController {
    @Autowired
    @Qualifier(value = "userService")
    UserService userService;

    @Autowired
    EmailService emailService;

    String title="请接收您的邮箱验证码";
    String content="欢迎使用大佬之路app,您的注册验证码是";

    ObjectMapper mapper = new ObjectMapper();

    @GetMapping("/user")
    public String login(@RequestParam String UserID, String UserKey) throws JsonProcessingException {
        List<UserBean> userInfo = userService.login(UserID, UserKey);
        String json = null;
        Result result=new Result();
        if (userInfo.size() == 1) {
            userInfo.get(0).setToken(BaseUtil.getUtil().getUUID());
            result.setState(200);
            result.setTime(BaseUtil.getUtil().getFormartTime(null));
            result.setMsg("请求成功");
            result.setData(userInfo.get(0));
            json = mapper.writeValueAsString(result);
            log.info("执行login方法,查询到的用户名是" + userInfo.get(0).getUserID());
        } else {
            throw new UserNotFoundException("The user did not find it.");
        }
        return json;
    }
    @PostMapping("/getMailCode")
    public String getMailCode(@RequestParam String mail) throws JsonProcessingException {
        String roundNum = BaseUtil.getUtil().getRoundNum(999999, 100000);
        Boolean sendMail = emailService.sendMail(mail, title, content + roundNum);
        Result result=new Result();
        if (sendMail){
            result.setState(200);
            result.setTime(BaseUtil.getUtil().getFormartTime(null));
            result.setMsg("验证码发送成功");
            String token = BaseUtil.getUtil().getUUID();
            result.setData(token);
            TokenCache.getSimpleCache().put(mail,token);
            TokenCache.getSimpleCache().put(mail+"token",roundNum);
            Map<String, String> all = TokenCache.getSimpleCache().getAll();
            System.out.println(all.toString());
        }else {
            result.setState(400);
            result.setTime(BaseUtil.getUtil().getFormartTime(null));
            result.setMsg("验证码发送失败");
        }
        return mapper.writeValueAsString(result);
    }

    @PostMapping(value = "/user")
    public String regin(@RequestParam String userID, String userKey, String userName,String codeNum,@RequestHeader String token) throws JsonProcessingException {
        boolean isusing = token.equals(TokenCache.getSimpleCache().get(userID));
        System.out.println(token);
        System.out.println(TokenCache.getSimpleCache().get(userID));
        Result result=new Result();
        if (isusing){
            System.out.println(codeNum);
            if (codeNum.equals(TokenCache.getSimpleCache().get(userID+"token"))) {
                String UserRtime = BaseUtil.getUtil().getFormartTime(null);
                int regin = userService.regin(userID, userKey, userName, UserRtime);
                if (regin == 1) {
                    result.setState(200);
                    result.setMsg("用户注册成功");
                    result.setTime(BaseUtil.getUtil().getFormartTime(null));
                    log.info("注册用户id:" + userID);
                    TokenCache.getSimpleCache().remove(userID);
                    TokenCache.getSimpleCache().remove(userID+"token");
                } else {
                    result.setState(400);
                    result.setMsg("用户注册失败");
                    result.setTime(BaseUtil.getUtil().getFormartTime(null));
                }
            }else {
                result.setState(400);
                result.setMsg("验证码错误,请重新填写");
                result.setTime(BaseUtil.getUtil().getFormartTime(null));
            }
        }else {
            result.setState(500);
            result.setMsg("Token验证失败,请重新填写");
            result.setTime(BaseUtil.getUtil().getFormartTime(null));
        }
        return mapper.writeValueAsString(result);
    }
}
